package com.example.gallery;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.board.R;

/*
public class ImageListAdapter extends ArrayAdapter<AlbumDTO>{
	String TAG = "ImageListAdapter";
	ArrayList<AlbumDTO> list;
	
	
	public ImageListAdapter(Context context, int imageRow, ArrayList<AlbumDTO> list) {
		super(context, imageRow, list);
		this.list = list;
	}


	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if(v == null){
			LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
			v= inflater.inflate(R.layout.image_row, null);
		}
		
		notifyDataSetChanged();
		AlbumDTO dto = new AlbumDTO();
		dto = list.get(position);
		notifyDataSetChanged();
		
		if(dto != null){
			ImageView iv = (ImageView) v.findViewById(R.id.iv_image);
			
			iv.setImageURI(Uri.parse(dto.a_image));
		}
		return v;
	}
}
*/



public class ImageListAdapter extends BaseAdapter implements OnCheckedChangeListener, OnClickListener{
	String TAG = "ImageListAdapter";
	int cnt = 0;	//checkd count

	private onTestClick ontest;
	
	int deviceWidth;
	int deviceHeight;
	int position;
	int IMAGE_NO;
	boolean[] isCheckedSetting;
	
	TextView tv_selected_images;
	Activity activity;
	LayoutInflater inflater;
	ArrayList<ImageDTO> list;
	Context mcontext ;			//상위 클래스로부터 context를 받음 -> imageview를 상위로 보내야 하므로
	CheckBox checkBox;
	OnClickListener callback;
	
	private static class ViewHolder{
		int position;
		ImageView imageview = null;
		CheckBox checkbox = null;
	}
	
	/*	
 	public ImageListAdapter(Context context, int position, OnClickListener callback){
	  	super();
		this.mcontext = context;
		this.position = position;
		this.callback = callback;
		
	}
	*/
	
	public ImageListAdapter(Context context, ArrayList<ImageDTO> list) {
		
		this.mcontext = context;
		this.list = list;
		isCheckedSetting = new boolean[list.size()];
		
		
		DisplayMetrics displayMetrics = new DisplayMetrics();
		
		inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		deviceWidth = displayMetrics.widthPixels;
		deviceHeight = displayMetrics.heightPixels;
	}
	
	public ImageListAdapter(Context context, ArrayList<ImageDTO> list, int position, OnClickListener callback) {
		
		this.mcontext = context;
		this.list = list;
		this.position = position;
		this.callback = callback;
		isCheckedSetting = new boolean[list.size()];
		
		DisplayMetrics displayMetrics = new DisplayMetrics();
		setCheckbox(position);
		inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		deviceWidth = displayMetrics.widthPixels;
		deviceHeight = displayMetrics.heightPixels;
	}
	
	public void myChecked(int position){
		isCheckedSetting[position] = !isCheckedSetting[position];
	}
	
	public int getCheckedCount(){
		int count = 0;
		for(int i=0; i<isCheckedSetting.length; i++){
			if(isCheckedSetting[i]){
				count++;
			}
		}
		return count;
	}
	
	@Override
	public int getCount() {
		int count = 0;
		count = list.size();
		return count;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		Log.i(TAG, "getItem : ["+position+"]" +list.get(position).NAME);
		
		return list.get(position).NAME;
	}
	
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		IMAGE_NO = list.get(position).NO;
		Log.i(TAG, "image_list 수 "+list.size());
//		Log.i(TAG, "getItemId : ["+position+"]- IMAGE_NO : " +IMAGE_NO);
//		setCheckbox(position);
		Log.i(TAG, "getItemId");
		return IMAGE_NO;
	}
	
	public void setCheckbox(int position){
		list.get(position).setChecked(true);
		Log.i(TAG, position +" item checked?? "+list.get(position).CHECKED);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		ImageView picture;
		CheckBox box;
			Log.i(TAG, "getView-- position : "+position);
			Log.i(TAG, getCheckedCount()+" 개 체크됨");
		if(convertView == null){
			convertView = inflater.inflate(R.layout.image_row, null);
			holder = new ViewHolder();
			holder.imageview = (ImageView)convertView.findViewById(R.id.iv_image);
			holder.checkbox = (CheckBox)convertView.findViewById(R.id.checkBox);
			
			
			if(callback != null){
				holder.checkbox.setVisibility(View.VISIBLE);
				tv_selected_images = (TextView)parent.findViewById(R.id.tv_selected_images);

//				holder.checkbox = (CheckBox) parent.getChildAt(position).findViewById(R.id.checkBox);
			}
				
			if(list.get(position).CHECKED){	// long clicked imageView
				holder.checkbox.setChecked(true);
//				CheckBox check_box = (CheckBox) convertView.findViewById(R.id.checkBox);
				Log.i(TAG, "onclick tag : "+				holder.checkbox.getTag());

//				list.get((Integer)check_box.getTag()).setChecked(true);
				
				/*if(check_box.isChecked()){
					Log.i(TAG, "checked image no: "+getItemId((Integer)check_box.getTag()));
					list.get((Integer)check_box.getTag()).setChecked(true);
				}
				else{
					Log.i(TAG, "unchecked image no: "+getItemId((Integer)check_box.getTag()));
					list.get((Integer)check_box.getTag()).setChecked(false);
				}*/
				
				int sum = 0;
				for(int i=0; i<list.size(); i++){
					if(list.get(i).isChecked()){
						sum++;
					}
				}
				cnt = sum;
				Log.i(TAG, "cnt : "+cnt);
				
				ontest.onClick();
			}
			
			
			holder.checkbox.setOnCheckedChangeListener(this);
			convertView.setTag(holder);
//			holder.imageview.setImageURI(Uri.parse(list.get(position).NAME));
			Log.i(TAG, "image_path : "+list.get(position).NAME );
			
		}
		else{
			holder = (ViewHolder)convertView.getTag();
		}
		
//		ImageDTO dto = (ImageDTO) getItem(position);
		
		holder.position = position;
		holder.imageview.setImageURI(Uri.parse(list.get(position).NAME));
//		holder.checkbox.setChecked(isCheckedSetting[position]);		//null
		holder.checkbox.setOnClickListener(this);
		
		holder.checkbox.setOnClickListener(onTestClick);
//		holder.imageview.setOnClickListener(this);
		
		holder.imageview.setTag(position);
		holder.checkbox.setTag(position);
		
		return convertView;
	}

	
	public ArrayList<ImageDTO> getCheckedImage(int position){
		ArrayList<ImageDTO> list = new ArrayList<ImageDTO>();
		for(int i=0; i<getCount(); i++){
			
		}
		return list;
	}
	
	//call whenever checked event
	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
//		Log.i(TAG, "onCheckedChanged position : " +position+", checked : " +isChecked);
		
		
		
	}

	@Override
	public void onClick(View v) {
		CheckBox box = (CheckBox) v.findViewById(R.id.checkBox);
		Log.i(TAG, "onclick tag : "+box.getTag());

		
		
		if(box.isChecked()){
			Log.i(TAG, "checked image no: "+getItemId((Integer)box.getTag()));
			list.get((Integer)box.getTag()).setChecked(true);
		}
		else{
			Log.i(TAG, "unchecked image no: "+getItemId((Integer)box.getTag()));
			list.get((Integer)box.getTag()).setChecked(false);
			
		}
		int sum = 0;
		for(int i=0; i<list.size(); i++){
			if(list.get(i).isChecked()){
				sum++;
			}
		}
		cnt=sum;
		Log.i(TAG, "cnt : "+cnt);
		tv_selected_images.setText(cnt+"");
	}
	
	private OnClickListener onTestClick = new OnClickListener() {
		
		@Override
		public void onClick(View paramView) {
			CheckBox box = (CheckBox) paramView.findViewById(R.id.checkBox);
			Log.i(TAG, "onclick tag : "+box.getTag());

			if(box.isChecked()){
				Log.i(TAG, "checked image no: "+getItemId((Integer)box.getTag()));
				list.get((Integer)box.getTag()).setChecked(true);
			}
			else{
				Log.i(TAG, "unchecked image no: "+getItemId((Integer)box.getTag()));
				list.get((Integer)box.getTag()).setChecked(false);
				
			}
			int sum = 0;
			for(int i=0; i<list.size(); i++){
				if(list.get(i).isChecked()){
					sum++;
				}
			}
			cnt=sum;
			Log.i(TAG, "cnt : "+cnt);
			
			ontest.onClick();
		}
	};
	
	public void setOnTestClickListener(onTestClick listener){
		ontest = listener;
	}
	
	public interface onTestClick{
		public void onClick();
	}

}
